//
//  HSYBaseTableViewController.m
//  高仿暴走斗图
//
//  Created by huangsongyao on 16/3/4.
//  Copyright © 2016年 huangsongyao. All rights reserved.
//

#import "HSYBaseTableViewController.h"

@implementation HSYBaseTableViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
}

#pragma mark - Create TableView

- (void)createTableView {
    
    [self createTableViewInViewWithIsHidLine:NO isScrollEnabled:YES style:UITableViewStylePlain];
}
- (void)createTableViewInViewWithIsHidLine:(BOOL)hidLine isScrollEnabled:(BOOL)scrollEnabled style:(UITableViewStyle)style {

    [self createTableViewInParentView:self.view rect:CGRectMake(0, 0, IPHONE_WIDTH, IPHONE_HEIGHT-[self getTopBarHeight]) isHidLine:hidLine isScrollEnabled:scrollEnabled style:style];
}

- (void)createTableViewInParentView:(UIView *)parentView rect:(CGRect)rect isHidLine:(BOOL)hidLine isScrollEnabled:(BOOL)scrollEnabled style:(UITableViewStyle)style {
    
    if (self.tableView || !parentView) {
        return;
    }
    
    self.tableView = [NSObject createTabelViewForFrame:rect
                                              andStyle:style
                                              delegate:self
                                            dataSource:self
                                      canClipsToBounds:YES
                                      canScrollEnabled:scrollEnabled
                                            isHideLine:hidLine
                                     withTableFootView:[[UIView alloc] initWithFrame:CGRectMake(0, 0, 1.0, 1.0)]];

    [parentView addSubview:self.tableView];
}


#pragma mark - UITableViewDelegate && UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    return nil;//UITableViewCellSelectionStyleNone
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    UITableViewCell *cell = [self getTableViewCellForIndexPath:indexPath withTableView:tableView];
    if (self.selectIndexPathCell) {
        self.selectIndexPathCell(cell, indexPath);
    }
}


#pragma mark - TableView Operation

- (NSIndexPath *)getTableViewIndexPathForCell:(UITableViewCell *)cell withTableView:(UITableView *)tableView {
    
    if (!cell || !tableView) {
        return nil;
    }
    
    NSIndexPath *indexPath = [tableView indexPathForCell:cell];
    return indexPath;
}


- (UITableViewCell *)getTableViewCellForIndexPath:(NSIndexPath *)indexPath withTableView:(UITableView *)tableView {
    
    if (!indexPath || !tableView) {
        return nil;
    }
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    return cell;
}

- (void)deleteRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowDeleteAnimation:(UITableViewRowAnimation)animation tableView:(UITableView *)tableView {
    
    if (!indexPaths || !tableView) {
        return;
    }
    [tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationTop];
}


#pragma mark - Pull Down && Up

- (void)addRefelsh {
    [self addPullDown];
    [self addPullUp];
}


#pragma mark - Pull Down

- (void)addPullDown {
    
    @weakify(self);
    [self addPullDownToScrollView:self.tableView withActionHandler:^{
        @strongify(self);
        [self pullDown];
    }];
}

- (void)pullDown {
    
    [self startPullDownAnimationToScrollView:self.tableView];
}

- (void)stopPullDown {
    
    @weakify(self);
    [self stopPullDownAnimationToBlock:^{
        @strongify(self);
        [self stopPullDownAnimationToScrollView:self.tableView];
    }];
}



#pragma mark - Pull Up

- (void)addPullUp {
    
    @weakify(self);
    [self addPullUpToScrollView:self.tableView withActionHandler:^{
        @strongify(self);
        [self pullUp];
    }];
}

- (void)pullUp {
    
    [self startPullUpAnimationToScrollView:self.tableView];
    
}


- (void)stopPullUp {
    
    @weakify(self);
    [self stopPullUpAnimationToBlock:^{
        @strongify(self);
        [self stopPullUpAnimationToScrollView:self.tableView];
    }];
}



@end
